{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Action2\n",
    "\n",
    "一个农民承包了6块耕地共300亩，准备播种小麦，玉米，水果和蔬菜四种农产品，已知各种农产品的计划播种面积、每块土地种植不同农产品的单产收益     如何进行合理安排，使得总收益最大     可以使用pulp/ortools工具"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "6\n",
      "flatten= <function farming_problem.<locals>.<lambda> at 0x000002724CA8BE50>\n",
      "[[x00, x01, x02, x03, x04, x05], [x10, x11, x12, x13, x14, x15], [x20, x21, x22, x23, x24, x25], [x30, x31, x32, x33, x34, x35]]\n"
     ]
    }
   ],
   "source": [
    "import pulp\n",
    "import numpy as np\n",
    "from pprint import pprint\n",
    "\n",
    "#定义问题\n",
    "def farming_problem(costs, x_max, y_max):\n",
    "    row = len(costs)\n",
    "    col = len(costs[0])\n",
    "    print(row)\n",
    "    print(col)\n",
    "    # 定义问题\n",
    "    prob = pulp.LpProblem('Farming', sense=pulp.LpMaximize)\n",
    "    # 定义决策变量\n",
    "    var = [[pulp.LpVariable(f'x{i}{j}', lowBound=0, cat=pulp.LpInteger) for j in range(col)] for i in range(row)]\n",
    "    #递归展开列表\n",
    "    flatten = lambda x: [y for l in x for y in flatten(l)] if type(x) is list else [x] \n",
    "    print('flatten=',flatten)\n",
    "    print(var)\n",
    "    \n",
    "    #定义目标函数\n",
    "    prob += pulp.lpDot(flatten(var), costs.flatten())\n",
    "    \n",
    "    #定义约束条件\n",
    "    for i in range(row):\n",
    "        prob += pulp.lpSum(var[i]) <= x_max[i]\n",
    "    for j in range(col):\n",
    "        prob += pulp.lpSum([var[i][j] for i in range(row)]) <= y_max[j]\n",
    "#         prob += pulp.lpSum(var[i][j]) <= y_max[i]\n",
    "    prob.solve()\n",
    "    \n",
    "    result = {'object':pulp.value(prob.objective),\n",
    "              'var':[[pulp.value(var[i][j]) for j in range(col)] for i in range(row)]}\n",
    "    return result\n",
    "\n",
    "\n",
    "costs = np.array([[500, 550, 630, 1000, 800, 700],\n",
    "                  [800, 700, 600, 950, 900, 930],\n",
    "                  [1000, 960, 840, 650, 600, 700],\n",
    "                  [1200, 1040, 980, 860, 880, 780]])\n",
    "#最大产量\n",
    "max_plant = [76,88,96,40]\n",
    "#最大耕种\n",
    "max_cultivation = [42,56,44,39,60,59]\n",
    "#使用线性规划\n",
    "result = farming_problem(costs, max_plant, max_cultivation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'object': 284230.0,\n",
       " 'var': [[0.0, 0.0, 6.0, 39.0, 31.0, 0.0],\n",
       "  [0.0, 0.0, 0.0, 0.0, 29.0, 59.0],\n",
       "  [2.0, 56.0, 38.0, 0.0, 0.0, 0.0],\n",
       "  [40.0, 0.0, 0.0, 0.0, 0.0, 0.0]]}"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最大值为284230.0\n",
      "各变量的取值为：\n",
      "[[0.0, 0.0, 6.0, 39.0, 31.0, 0.0],\n",
      " [0.0, 0.0, 0.0, 0.0, 29.0, 59.0],\n",
      " [2.0, 56.0, 38.0, 0.0, 0.0, 0.0],\n",
      " [40.0, 0.0, 0.0, 0.0, 0.0, 0.0]]\n"
     ]
    }
   ],
   "source": [
    "print(f'最大值为{result[\"object\"]}')\n",
    "print('各变量的取值为：')\n",
    "pprint(result['var'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "第一块第4个土地小麦种了39个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.0 64-bit ('Bi_env': venv)",
   "language": "python",
   "name": "python38064bitbienvvenvba07af95a1bb4b078aa8134bba84dff2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "165px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
